﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI;
using Delta_util.Clases;

namespace Delta_editoresWeb.GridColumns
{
    public class ColumnaDocs : TemplateField
    {
        public string DataField { get; set; }

        public ColumnaDocs()
        {

            this.ItemTemplate = new ItemDocsTemplate(this);
        }
    }

    public class ItemDocsTemplate : ITemplate
    {
        protected ColumnaDocs datos;
        Image lblinfo = null;
        Literal ltlink = null;
        public ItemDocsTemplate(ColumnaDocs padre) { datos = padre; }


        public void InstantiateIn(System.Web.UI.Control container)
        {
            ltlink = new Literal() { Text = "" };
            Literal ltlinkFinal = new Literal() { Text = "</a>" };
            lblinfo = new Image() { Width = 20, Height = 20, ImageAlign = ImageAlign.Middle };
            lblinfo.DataBinding += OnDataBinding;
            ltlink.DataBinding += new EventHandler(ltlink_DataBinding);
            container.Controls.Add(ltlink);
            container.Controls.Add(lblinfo);
            container.Controls.Add(ltlinkFinal);

        }

        void ltlink_DataBinding(object sender, EventArgs e)
        {
            Literal ctrl = (Literal)sender;

        }

        private void OnDataBinding(object sender, EventArgs e)
        {
            Image ctrl = (Image)sender;

            object bound_value_obj = null;

            IDataItemContainer data_item_container = (IDataItemContainer)ctrl.NamingContainer;
            GridView grd = (data_item_container as GridViewRow).Parent.Parent as GridView;

            bound_value_obj = DataBinder.Eval(data_item_container.DataItem, datos.DataField);
            if (bound_value_obj != null && !string.IsNullOrWhiteSpace(bound_value_obj.ToString()))
            {
                string tipoArchivo = bound_value_obj.ToString().Substring(bound_value_obj.ToString().LastIndexOf('.') + 1).ToLower();
                ctrl.ImageUrl = ctrl.Page.ClientScript.GetWebResourceUrl(this.GetType(), "Delta_editoresWeb.Imagenes." + tipoArchivo + ".png");
                ltlink.Text = "<a href='" + bound_value_obj.ToString().Replace("~/", Utilerias.ObtenerHttpServer(ctrl.Page)) + "' target='_blank'>";
            }
            else
                ctrl.Visible = false;


        }

    }
}
